package ar.com.ford.it.fullEconomics.persistence.dao;
 
import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class SupplierDao extends Dao {
    private static final String CLASS_NAME = SupplierDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Elimina de forma definitiva de la base de datos el Supplier pasado
     * por parametro
     * 
     * @param Supplier
     * Debe haber sido obtenido de la base de datos
     * @throws PersistenceException 
     * @throws PersistenceException
     */
    public void deleteSupplier(Teess01Supplier supplier) throws PersistenceException {
        String METHOD_NAME = "deleteSupplier";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(supplier);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }
/**
 * Crea en proveedor con los datos obtenidos
 * @param supplier
 * @throws PersistenceException
 */
    public void createSupplier(Teess01Supplier supplier) throws PersistenceException {
        String METHOD_NAME = "createSupplier";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(supplier);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }
    /**
     * Busca lossupplier asociado al codigo y/o nombre
     * @param supplier
     * @return
     * @throws PersistenceException 
     */
    @SuppressWarnings("unchecked")
    public Collection<Teess01Supplier> findSupplier (Teess01Supplier supplier) throws PersistenceException {
        String METHOD_NAME = "findSupplier";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teess01Supplier> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findSupplier",
                Teess01Supplier.class);
        query.setParameters(supplier);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
        
    }
/**
 * Consulta si hay algun supplier con ese nombre
 * @param name
 * @return boolean
 * @throws PersistenceException
 */
    public boolean supplierNameAlreadyExists(Teess01Supplier supplier) throws PersistenceException {
        String METHOD_NAME = "supplierNameAlreadyExists";
        log.entering(CLASS_NAME, METHOD_NAME);
        Object result;
        boolean resp=false;
        PersistenceManager pm = null;
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("supplierNameAlreadyExists",
                Teess01Supplier.class);
        query.setParameters(supplier);

        result =  pm.executeQuery(query);
        
        if(result!=null) {
            resp=true;
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
        return resp;
    }
/**
 * Retorna objeto para actualizacion
 * @param supplier
 * @return
 * @throws PersistenceException
 */
    public Teess01Supplier getUpdateSupplier(Teess01Supplier supplier) throws PersistenceException {

        String METHOD_NAME = "getUpdateSupplier";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teess01Supplier result = null;

        
        result = (Teess01Supplier) super.getObjForUpdate(supplier);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;

        
    }

}
